package ex303.shape;

import java.util.Objects;

public class RegularPolygon {
    private int numberOfSides;
    private double lengthOfSide;
    private double x;
    private double y;

    public RegularPolygon() {
        this(3, 1.0, 0.0, 0.0);
    }

    public RegularPolygon(int numberOfSides, double lengthOfSide) {
        this(numberOfSides, lengthOfSide, 0, 0);
    }

    public RegularPolygon(int numberOfSides, double lengthOfSide, double x, double y) {
        this.numberOfSides = numberOfSides;
        this.lengthOfSide = lengthOfSide;
        this.x = x;
        this.y = y;
    }

    public double getArea() {
        return numberOfSides * lengthOfSide * lengthOfSide / (4 * Math.tan(Math.PI / numberOfSides));
    }

    public double getOffset() {
        return Math.sqrt(x * x + y * y);
    }

    @Override
    public String toString() {
        return String.format("[%d,%.2f]@(%.2f,%.2f)", numberOfSides, lengthOfSide, x, y);
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) return true;
        if (other == null || getClass() != other.getClass()) return false;

        RegularPolygon that = (RegularPolygon) other;

        if(this.numberOfSides != that.numberOfSides) return false;
        if(Double.compare(that.lengthOfSide, this.lengthOfSide) != 0) return false;
        if(Double.compare(that.x, this.x) != 0) return false;
        if(Double.compare(that.y, this.y) != 0) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(numberOfSides, lengthOfSide, x, y);
    }

    public int getNumberOfSides() {
        return numberOfSides;
    }

    public void setNumberOfSides(int numberOfSides) {
        this.numberOfSides = numberOfSides;
    }

    public double getLengthOfSide() {
        return lengthOfSide;
    }

    public void setLengthOfSide(double lengthOfSide) {
        this.lengthOfSide = lengthOfSide;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
}
